var app = getApp();
var QQMapWX = require('../../../common/qqmap-wx-jssdk.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: 'ZHBBZ-2MX6F-MMHJS-J6KN6-NGS5H-TLBWG'
});
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   *
   *  组件的属性列表
   */
  properties: {
    // 弹窗标题
    title: {
      type: String,
      value: '标题' // 默认值
    },
    // 弹窗内容
    content: {
      type: 'String',
      value: '弹窗内容'
    },

    // 弹窗确认按钮文字
    confirmText: {
      type: 'String',
      value: '确定'
    }
  },
  /**
   * 组件内私有数据
   */
  data: {
    // 弹窗显示控制
    isShow: false,
    callback: false
  },
  onLoad: function(options) {
  },
  /**
   * 组件的公有方法列表
   */
  methods: {

    //隐藏弹框
    hideDialog() {
      this.setData({
        isShow: !this.data.isShow
      })
    },
    //展示弹框,在这里判断是否需要登陆，是否已经登陆过了
    showDialog(callback) {
      if (callback) {
        this.setData({
          callback: callback
        })
      }
      //是否登陆
      if (app.globalData.token === null) {
        //未登陆
        this.setData({
          isShow: !this.data.isShow
        })
      } else {
        //已登陆
        this.returnFn(app.globalData.city);
      }
    },
    /**
     * triggerEvent 组件之间通信
     */
    confirmEvent() {
      console.log('in plus');
      this.triggerEvent("confirmEvent");
    },
    returnFn(logined) {
      this.triggerEvent("bindGetUserInfo", logined);

      if (this.data.callback) {
        this.data.callback(logined)
      }
    },
    bindGetUserInfo(a) {
      console.log('inplus');
      var detail = a.detail;
      if (detail.errMsg == "getUserInfo:ok") {
        console.log('login ok');
        this.initLogin(detail);
      } else {
        console.log('login error:' + detail.errMsg);
        app.alert('授权失败，尝试重新进入或联系客服');
        this.returnFn(false);
      }
    },
    initLogin(detail) {
      app.loading();
      var that = this;
      //取设备信息
      app.globalData.systemInfo = wx.getSystemInfoSync();
      wx.setStorageSync('systemInfo', app.globalData.systemInfo);
      wx.login({
        success: function(res) {
          // 微信code
          var wxcode = res.code;
          //获取用户基本信息和敏感信息

          //用户基本信息
          if (app.globalData.userInfo === null) {
            app.globalData.userInfo = detail.userInfo;
            wx.setStorageSync('userInfo', detail.userInfo);
          }

          //服务登陆
          app.apiPost(app.apiList.login, {
            code: wxcode,
            rawData: detail.rawData,
            signature: detail.signature,
            encryptedData: detail.encryptedData,
            vi: detail.iv,
          }, function(res) {

            if (res.code == 200) {
              app.globalData.token = res.data.token;
              wx.setStorageSync('token', res.data.token);
              var city; //用户城市
              if (!res.data.userinfo.city.title) {
                wx.getLocation({
                  type: 'wgs84',
                  altitude: false,
                  success: function(res) {
                    //根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
                    qqmapsdk.reverseGeocoder({
                      location: {
                        latitude: res.latitude,
                        longitude: res.longitude
                      },
                      success: function(addressRes) {
                        city = addressRes.result.address_component.city;
                        app.globalData.city = city;
                        wx.setStorageSync('city', city);
                        that.returnFn(city);
                      }
                    })
                  },
                  fail: function(res) {},
                  complete: function(res) {},
                })
              } else {
                city = res.data.userinfo.city.title;
                app.globalData.city = city;
                wx.setStorageSync('city', city);
                that.returnFn(city);
              }
              app.hideloading();
              that.hideDialog();
            }
          });

        },
        fail: function(res) {
          console.log('微信登录请求失败')
        },
        // complete: function (res) {}
      })
    }
  }
})